一、Spring.NET 简单使用

最近用到了 Spring.NET ,不过在第一次使用的时候就遇到了一些问题,打算整理成系列博客,不断的总结和分享。

1. 什么是 Spring.NET ?

Spring 框架本是 Java 平台上一个应用非常多的、开源的框架。虽然语言是固定的,但是好的方法应该是通用的,于是 Spring 框架 就被程序员从 Java 平台搬迁到了 .NET 平台。

通过Spring.NET,我们可以用统一且透明的方式来配置应用程序。Spring.NET 的重点是为中间层提供声明式事务管理,以及一个功能齐全的 ASP.NET 扩展框架。Spring.NET 是非侵入式的,代码对框架本身不会产生任何依赖。

Spring.NET 能够提供很多方面的功能,例如:控制反转(英文缩写为IoC)、依赖注入(DI)、面向方面编程(AOP)、数据访问抽象, 以及 ASP.NET 集成等。

Spring.NET 核心:

Spring.Core 库是框架的基础, 提供依赖注入功能。Spring NET中大多数类库依赖或扩展了Spring.Core的功能。IObjectFactory接口提供了一个简单而优雅的工厂模式,移除了对单例和一些服务定位stub的必要。允许你将真正的程序逻辑与配置解耦。作为对IObjectFactory 的扩展,IApplicationContext接口也在Spring.Core库中。

2.快速创建第一个使用 Spring.NET 的程序

本次开发环境: VS 2017 本次开发项目:.netframework控制台项目

(1). 使用 Nuget 安装 Spring.core

使用 Spring.NET 需要 Spring.Core 库的支持,同时在 Nuget 中安装 Spring.Core 包,会在项目上自动引入相关的引用。

(2).创建相关文件。

本次创建两个文件:IUserInfo 接口、UserInfo 实体类。使用 Spring 反射创建 UserInfo 类,使用 IUserInfo 接口调用反射创建的类。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//IUserInfo接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo
{
public interface IUserInfo
{
string ShowMss();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//UserInfo实体类 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo
{
//实现 IUserInfo接口
public class UserInfo : IUserInfo
{
public string ShowMss()
{
return "Hello Spring.NET";
}
}
}

重要的是在配置节里面的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<!--sectionGroup节点一定要在紧跟着configuration下面第一个添加-->
<sectionGroup name="spring">
<!--跟下面Spring.Net节点配置是一一对应关系-->
<section name="context" type="Spring.Context.Support.ContextHandler,Spring.Core"/>
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler,Spring.Core"/>
</sectionGroup>
</configSections>

<!--Spring.Net节点配置-->
<spring>
<!--容器配置-->
<context>
<resource uri="config://spring/objects"/>
</context>
<objects>
<!--objects里面放容器的所有节点-->
<description>An example that demonstrates simple Ioc features.</description><!--描述-->
<!--name 必须要唯一的,type = 类的全名称,所在的程序集-->
<object name="UserInfo" type="Demo.UserInfo,Demo"></object><!--咱们刚才创建的UserInfo实体类-->
</objects>
</spring>

<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>
</configuration>

获取并执行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Program 程序入口类
//引用 : Spring.Context 和 Spring.Context.Support 两个命名空间
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spring.Context;
using Spring.Context.Support;

namespace Demo
{
class Program
{
static void Main(string[] args)
{
IApplicationContext ctx = ContextRegistry.GetContext();
IUserInfo userInfo = (IUserInfo)ctx.GetObject("UserInfo");
Console.WriteLine(userInfo.ShowMsg());
Console.Read();
}
}
}

输出结果:

输出结果
输出结果

注意事项:

  1. 必须安装 Spring.core 包,否则缺少环境支持。
  2. 需要正确 配置 配置文件:
    1. sectionGroup节点一定要在紧跟着configuration下面第一个添加.
    2. objects 节点中的 <object></object>节点,name="value" value 值一定要唯一,type ="value1 , value2"value1 是需要反射创建出来的类的全名称value2该类所在的程序集
    3. 反射创建。

二、Spring.NET在MVC中的使用

在ASP.NET MVC中应该如何使用Spring.NET?

1.先导入dll文件

导入以下这些程序节

  • Spring.core.dll
  • Spring.Web.dll
  • Spring.Web.Extensions.dll
  • Spring.Web.Mvc4.dll
  • Common.Logging.dll

或者直接在 NuGet 中安装 Spring.Web.MVC , 这个方法会直接应用相应的 dll。

2.修改配置信息

在大型的项目中,如果把所有的配置信息都放在 Web.config 文件夹,会让该文件显得非常复杂,也不利于维护和修改,这个时候可以将 Spring 的配置节分离到单独的 Xml 文档中。

Web.Config文件相应配置如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!--Spring.NET配置节信息-->
<sectionGroup name="spring">
<!--Spring.Net配置-->
<section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc4"/>
</sectionGroup>
<!--Spring.NET配置节信息END-->
</configSections>
<!--Spring配置信息-->
<spring>
<!--Spring.Net配置-->
<context>
<resource uri="file://~/Config/controllers.xml"/>
<resource uri="file://~/Config/services.xml"/>
</context>
</spring>
<!--Spring配置信息End-->

3.准备分离出来的文件

在 Web 项目下新建一个名叫 Config 的文件夹,里面存放 Spring的 配置信息节。

需要注意的是,该文件夹里面的 Xml 文件的复制到输出目录属性需要修改为始终复制。这样做的原因是因为这个Xml文件是静态文件,项目生成解决方案的时候默认是不加载的,只有始终复制才会每次生成的时候加载到 Debug文件夹内。

4.修改controllers.xml中的配置信息

在 controllers.xml 文件中,根节点为 Objects 节点,

1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object type="LRS.OA.Web.Controllers.UserInfoController,LRS.OA.Web" singleton="false">
<property name="UserInfoService" ref="UserInfoService"/>
</object>
</objects>
  • type : 容器内的对象所在的位置(命名空间.类)
  • singleton :是否指定单例模式,默认是单例模式
  • propeerty : DI 注入的属性
  • ref : propeerty 节的属性,指向的是创建的<object>节点名称,也就是创建的其他的对象节点

services.xml

1
2
3
4
5
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object name="UserInfoService" type="LRS.OA.BLL.UserInfoService,LRS.OA.BLL" singleton="false">
</object>
</objects>

### 6.修改Global文件

将 MVCApplication 继承自 SpringMvcApplication 。

三、Ioc 和 DI

Ioc 是控制反转,目的是将实例化类这一步骤从代码中取出,让容器完成自动完成该操作。

DI是依赖注入,是容器在实例化对象时,将该实例的指定属性赋上指定的值。